package com.zzptc.llq.bdsjws.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.zzptc.llq.bdsjws.MyApplication;

import com.zzptc.llq.bdsjws.R;
import com.zzptc.llq.bdsjws.activities.FamilyProtectctivity;
import com.zzptc.llq.bdsjws.activities.MainActivity;
import com.zzptc.llq.bdsjws.vo.Box;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/5/24.
 */

public class BbxAdapter extends RecyclerView.Adapter<BbxAdapter.ViewHolder>{
     List<Box> list;
    public BbxAdapter(List<Box> list){
        this.list=list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.bbx_item,parent,false);
        ViewHolder viewHolder=new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final Box b=list.get(position);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(b.getName().equals("家人守护")){

                    Intent intent=new Intent(MyApplication.getContext(), FamilyProtectctivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApplication.getContext().startActivity(intent);
                }
                else if (b.getName().equals("回首页")){
                    Intent intent=new Intent(MyApplication.getContext(), MainActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApplication.getContext().startActivity(intent);
                }

            }
        });

      final   Box box=list.get(position);
        //先解决  从数据库取得的字符串  转成Drawable  准备：放图片到res
        Context context= MyApplication.getContext();
        int pic_int=context.getResources().getIdentifier(box.getIcon(),"mipmap",context.getPackageName());
    try {
        Drawable drawable= ContextCompat.getDrawable(context,pic_int);
        holder.iv_pic.setImageDrawable(drawable);
        holder.tv_name.setText(box.getName());
    }catch (Exception e){
        e.fillInStackTrace();
    }

    }

    @Override
    public int getItemCount() {
        return list.size();
    }
//内部内代表每一项
    public class ViewHolder extends RecyclerView.ViewHolder{
            @BindView(R.id.bbx_pic)
            ImageView iv_pic;
            @BindView(R.id.bbx_name)
            TextView tv_name;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

}
